package com.cqupt.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularMatcher {

    /**
     *  将问题键值转换为公共键值表达式
     * @param knowledge_key
     * @return
     */
    public static String RE_Matcher(String knowledge_key) {
        String content = knowledge_key;
        // 正则匹配 and、or 连接词
        Pattern pattern = Pattern.compile("\\sand\\s|\\sor\\s");
        Matcher matcher = pattern.matcher(content);
        List<String> operation = new ArrayList<>();
        while(matcher.find()){
            operation.add(matcher.group(0));
        }
        // 正则匹配 and、or 连接词
        String regex = "\\sand\\s|\\sor\\s|\\snot\\s";
        String[] keysArr = content.split(regex);
        // 1.先替换主机名
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_hostname = "\\{[A-Za-z0-9_]+:";
            pattern = Pattern.compile(regex_hostname);
            matcher = pattern.matcher(str);
            // 匹配操作
            // 匹配操作
            while(matcher.find()) {
                // 替换主机名
                keysArr[i] = matcher.replaceAll("\\{HostName:");
            }
        }
        // 2.先替换自动发现磁盘名称
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\[[A-Za-z0-9]+:,";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll("\\[DisName:,");
            }
        }
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\"[a-zA-z0-9]+:\"";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll("\"DisName:\"");
            }
        }
        // 3.替换阈值
        // 不等于 替换为 not equal
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\}\\<\\>[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll("} not equal VALUE");
            }
        }
        // 大于 大于等于 替换为 large than
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "(\\>|\\>\\=)[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(" large than VALUE");
            }
        }
        // 小于 小于等于 替换为 small than
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "(\\<|\\<\\=)[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(" small than VALUE");
            }
        }
        // 等于 替换为 equal
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            // 当前待匹配字符串
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\}\\=[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll("} equal VALUE");
            }
        }


        // 此处替换所有的触发器函数，可优化
        // 将触发器函数（last）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.last\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".last(args)");
            }
        }

        // 将触发器函数（avg）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.avg\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".avg(args)");
            }
        }

        // 将触发器函数（delta）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.delta\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".delta(args)");
            }
        }

        // 将触发器函数（fuzzytime）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.fuzzytime\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".fuzzytime(args)");
            }
        }

        // 将触发器函数（max）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.max\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".max(args)");
            }
        }

        // 将触发器函数（min）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.min\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".min(args)");
            }
        }

        // 将触发器函数（nodata）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.nodata\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".nodata(args)");
            }
        }

        // 将触发器函数（str）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.str\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".str(args)");
            }
        }

        // 将触发器函数（strlen）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.strlen\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".strlen(args)");
            }
        }

        // 将触发器函数（sum）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.sum\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".sum(args)");
            }
        }

        // 将触发器函数（timeleft）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.timeleft\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".timeleft(args)");
            }
        }

        // 正则匹配网络设备相关知识
        // 替换端口编号
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.[0-9]+\\]\\.";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".IFindex].");
            }
        }

        // 替换端口编码
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "[a-zA-Z0-9]+[0-9]+\\/[0-9]+\\/[0-9]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll("PortCode");
            }
        }

        StringBuilder result_str = new StringBuilder();
        for (int i = 0; i < keysArr.length-1; i++) {
            String temp_content = keysArr[i];
            result_str.append(temp_content);
            // 利用符号 | 分割
            result_str.append("|");
        }
        String last_content = keysArr[keysArr.length-1];
        result_str.append(last_content);
        return result_str.toString();
    }

    // 验证是主机名称或者是ip
    public static String filterRE_matcher(String queryString){
        String content = queryString;
        // 正则匹配主机名
        //Pattern pattern_name = Pattern.compile("^(?![a-zA-Z]*$)(?![0-9]*$)[a-zA-Z]{1}[a-zA-Z0-9]{5,19}$");
        //Matcher matcher_name = pattern_name.matcher(content);
        //if (matcher_name.matches() == true) {
        //    return "hostname";
        //}
        // 正则匹配ip地址
        Pattern pattern_ip = Pattern.compile("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])$");
        Matcher matcher_ip = pattern_ip.matcher(content);
        if (matcher_ip.matches() == true) {
            return "ip";
        }
        // 若匹配失败返回error
        return "hostname";
    }

    // 去掉主机维度通过模板匹配字符串
    public static String  RE_Matcher_v2(String tempalte_key){
        String content = tempalte_key;

        // 预处理字符串(自动发现规则)
        content = content.replace("{#FSNAME}","DisName");
        content = content.replace("{#SNMPINDEX}","IFindex");
        content = content.replace("{#IFNAME}","PortCode");
        content = content.replace("{#DEVNAME}","DevName");
        content = content.replace("{#SERVICE.NAME}","ServiceName");

        Pattern pattern = Pattern.compile("\\sand\\s|\\sor\\s|\\snot\\s");
        Matcher matcher = pattern.matcher(content);
        List<String> operation = new ArrayList<>();
        while(matcher.find()){
            operation.add(matcher.group(0));
        }

        // 正则匹配 and、or 连接词
        String regex = "\\sand\\s|\\sor\\s|\\snot\\s";
        String[] keysArr = content.split(regex);
        // 1.先替换模板名
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_hostname = "\\{[A-Za-z0-9\\s]+:";
            pattern = Pattern.compile(regex_hostname);
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                // 替换主机名
                keysArr[i] = matcher.replaceAll("\\{HostName:");
            }
        }
        // 2.替换阈值
        // 不等于 替换为 not equal
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\}\\<\\>[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll("} not equal VALUE");
            }
        }
        // 大于 大于等于 替换为 large than
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "(\\>|\\>\\=)[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(" large than VALUE");
            }
        }
        // 小于 小于等于 替换为 small than
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "(\\<|\\<\\=)[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(" small than VALUE");
            }
        }
        // 等于 替换为 equal
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\}\\=[\\{\\}a-zA-Z0-9\\.\\:\\/\\*\\-\\(\\)\\$\\\"\\[\\]]+";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll("} equal VALUE");
            }
        }

        // 3.此处替换所有的触发器函数，可优化
        // 将触发器函数（last）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.last\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".last(args)");
            }
        }

        // 将触发器函数（avg）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.avg\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".avg(args)");
            }
        }

        // 将触发器函数（delta）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.delta\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".delta(args)");
            }
        }

        // 将触发器函数（fuzzytime）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.fuzzytime\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".fuzzytime(args)");
            }
        }

        // 将触发器函数（max）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.max\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".max(args)");
            }
        }

        // 将触发器函数（min）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.min\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".min(args)");
            }
        }

        // 将触发器函数（nodata）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.nodata\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".nodata(args)");
            }
        }

        // 将触发器函数（str）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.str\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".str(args)");
            }
        }

        // 将触发器函数（strlen）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.strlen\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            // 匹配操作
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".strlen(args)");
            }
        }

        // 将触发器函数（sum）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.sum\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".sum(args)");
            }
        }

        // 将触发器函数（timeleft）替换为统一字符串
        for (int i = 0; i < keysArr.length; i++) {
            // 当前待匹配字符串
            String str = keysArr[i];
            String regex_filename = "\\.timeleft\\([0-9a-zA-Z\\#\\,\\{\\}\\$\\.]*\\)";
            // 构造Pattern
            pattern = Pattern.compile(regex_filename);
            // 获取matcger进行匹配
            matcher = pattern.matcher(str);
            // 匹配操作
            // 匹配操作
            while(matcher.find()) {
                keysArr[i] = matcher.replaceAll(".timeleft(args)");
            }
        }

        StringBuilder result_str = new StringBuilder();
        for (int i = 0; i < keysArr.length-1; i++) {
            String temp_content = keysArr[i];
            String temp_operation = operation.get(i);
            //if (i != 0) {
            //    result_str.append(' ');
            //}
            result_str.append(temp_content);
            //result_str.append(' ');
            result_str.append("|");
        }
        String last_content = keysArr[keysArr.length-1];
        //result_str.append(' ');
        result_str.append(last_content);
        return result_str.toString();
    }
}
